package org.csu.small.controller.front;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.csu.small.common.Constant;
import org.csu.small.common.ResponseCode;
import org.csu.small.common.UniformResponse;
import org.csu.small.entity.Shipping;
import org.csu.small.entity.User;
import org.csu.small.service.IShippingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author Rambo
 * @date 2020-06-08 9:02
 */
@Controller
@RequestMapping("/shipping/")
@Api(value = "API - ShippingController", description = "收货地址模块接口详情")
public class ShippingController {

    @Autowired
    private IShippingService shippingService;

    @PostMapping("add_new_shipping")
    @ResponseBody
    @ApiOperation(value = "新增用户收货地址")
    public UniformResponse<String>  addNewShipping(Shipping shipping,HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user != null){
            shipping.setUserId(user.getId().longValue());
            return  shippingService.addNewShipping(shipping);
        }
        return UniformResponse.createForErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getDescription());
    }

    @GetMapping("get_shipping")
    @ResponseBody
    @ApiOperation(value = "查询用户收货地址列表")
    public UniformResponse<List<Shipping>>  getShippingList(HttpSession session){
        User user = (User) session.getAttribute(Constant.LOGIN_USER);
        if(user != null){
            return shippingService.getShippingList(user.getId());
        }
        return UniformResponse.createForErrorMessage("用户未登录");
    }

    @PostMapping("edit_shipping")
    @ResponseBody
    @ApiOperation(value = "修改用户收货地址")
    public UniformResponse<String>  editShipping(Shipping shipping){

        return shippingService.editShipping(shipping);
    }

    @PostMapping("delete_shipping")
    @ResponseBody
    @ApiOperation(value = "删除用户收货地址")
    public UniformResponse<String>  deleteShipping(Integer shippingId){

        return shippingService.deleteShipping(shippingId);
    }

    @GetMapping("select_shipping")
    @ResponseBody
    @ApiOperation(value = "获取获取要编辑的收货人收货信息")
    public UniformResponse<Shipping>  selectShipping(Integer shippingId){
        return shippingService.selectShipping(shippingId);
    }

}
